package com.yachtar.property.db;

import android.database.sqlite.SQLiteDatabase;
import android.os.Handler;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class MySqlHelper {
	public final static String DbPath = "/mnt/sdcard/YiChengDa";
	public final static int ReadError = -1;
	public final static int ReadProgressRate = 1;
	public SQLiteDatabase db;
	public boolean isReady = false;

	public MySqlHelper() {
		try {
			initDb();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

	}

	private void initDb() throws IOException {
		File file = new File(DbPath + "/UseItem.db");
		if (file != null && file.exists()) {
			db = SQLiteDatabase.openOrCreateDatabase(file, null);
			isReady = true;
		} else {
			throw new IOException("没有数据，请导入数据库");
		}
	}

	public void readDatabaseIn(File dbFile, Handler handler) throws IOException {
		if (dbFile != null && dbFile.exists()) {
			File file = new File(DbPath);
			if (!file.exists()) {
				file.mkdirs();
			}
			file = new File(DbPath + "/UseItem.db");
			if (file.exists()) {
				file.delete();
			}
			boolean isSuccess = file.createNewFile();
			if (isSuccess) {
				InputStream in = new FileInputStream(dbFile);
				OutputStream out = new FileOutputStream(file);
				long totalLen = dbFile.length();
				byte[] buffer = new byte[1024];
				int tempLen = 0;
				long currentLen = tempLen;
				while ((tempLen = (in.read(buffer))) > 0) {
					out.write(buffer, 0, tempLen);
					currentLen += tempLen;
					handler.sendMessage(handler.obtainMessage(ReadProgressRate,
							Double.valueOf((currentLen * 1.0) / totalLen)));
				}
				in.close();
				out.flush();
				out.close();
				initDb();
				handler.sendMessage(handler.obtainMessage(ReadProgressRate,
						Double.valueOf(2)));
			} else {
				throw new IOException("创建新数据失败");
			}
		} else {
			throw new IOException("无效的数据源");
		}
	}

	public void writeDatabaseOut(File dbFile, Handler handler)
			throws IOException {
		if (dbFile != null && dbFile.exists()) {
			dbFile.delete();
		}
		boolean isS = dbFile.createNewFile();
		if (isS) {
			File file = new File(DbPath + "/UseItem.db");
			if (file != null && file.exists()) {
				OutputStream out = new FileOutputStream(dbFile);
				InputStream in = new FileInputStream(file);
				long totalLen = file.length();
				byte[] buffer = new byte[1024];
				int tempLen = 0;
				long currentLen = tempLen;
				while ((tempLen = in.read(buffer)) > 0) {
					out.write(buffer, 0, tempLen);
					currentLen += tempLen;
					handler.sendMessage(handler.obtainMessage(ReadProgressRate,
							Double.valueOf((currentLen * 1.0) / totalLen)));
				}
				in.close();
				out.flush();
				out.close();
				initDb();
				handler.sendMessage(handler.obtainMessage(ReadProgressRate,
						Double.valueOf(2)));
			} else {
				throw new IOException("数据源不存在");
			}
		} else {
			throw new IOException("无效的目录");
		}

	}

}
